<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>税务稽查助手-主页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/layui/v11/css/layui.css">
    <link rel="stylesheet" href="/layui/v2.6/adminui/dist/css/admin.css">

    <style>
        /* 确保根元素充满视口 */
        html, body {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

        /* 主布局容器 */
        .layui-layout-admin {
            position: relative;
            height: 100vh;
            width: 100vw;
        }

        /* 内容区基础样式 */
        #LAY_app_body {
            margin-top: 20px;
            position: absolute;
            overflow: auto;
            background: #f2f2f2;
            transition: all 0.3s; /* 平滑过渡效果 */
        }

        /* iframe完美适配 */
        .layadmin-iframe {
            width: 100%;
            height: 100%;
            border: 0;
            display: block;
        }
    </style>

</head>
<!--参考 layui 后台布局-->
<!--参考 layui 后台布局-->
<body class="layui-layout-body" id="LAY_home_iframe">
<div id="LAY_app" style="visibility: hidden">
    <div class="layui-layout layui-layout-admin">
        <!-- 头部区域 -->
        <div class="layui-header">
            <ul class="layui-nav layui-layout-left">
                <li class="layui-nav-item layadmin-flexible" lay-unselect>
                    <a href="javascript:void(0);" layadmin-event="flexible" title="侧边伸缩">
                        <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible"></i>
                    </a>
                </li>
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:void(0);" layadmin-event="refresh" title="刷新">
                        <i class="layui-icon layui-icon-refresh-3"></i>
                    </a>
                </li>
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <label for="menuSearch"></label>
                    <input id="menuSearch" type="text" placeholder="搜索菜单..." autocomplete="off"
                           class="layui-input layui-input-search">
                </li>
            </ul>
            <ul class="layui-nav layui-layout-right" lay-filter="layadmin-layout-right" style="margin-right: 20px;">
                <li class="layui-nav-item" lay-unselect>
                    <a lay-href="workrisk/riskPage11" layadmin-event="message" lay-text="消息中心">
                        <i class="layui-icon layui-icon-notice"></i>
                        <!-- 如果有新消息，则显示小圆点 -->
                        <span class="layui-badge-dot"></span>
                    </a>
                </li>
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="javascript:void(0);" layadmin-event="note">
                        <i class="layui-icon layui-icon-note"></i>
                    </a>
                </li>
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="javascript:void(0);" layadmin-event="theme">
                        <i class="layui-icon layui-icon-theme"></i>
                    </a>
                </li>
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="javascript:void(0);" layadmin-event="fullscreen">
                        <i class="layui-icon layui-icon-screen-full"></i>
                    </a>
                </li>
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:void(0);" style="margin-right: 10px">
                        <div>
                            <span id="deptName"></span>
                            <span>-</span>
                            <span id="nickName"></span>
                        </div>
                    </a>
                    <dl class="layui-nav-child">
                        <dd>
                            <a href="javascript:void(0);" data-id="identityProper" data-title="切换身份"
                               lay-href="users/identity" class="menuNvaBar">切换身份</a>
                        </dd>
                        <dd><a lay-href="users/info">基本资料</a></dd>
                        <dd><a lay-href="users/password">修改密码</a></dd>
                        <hr>
                        <dd id="logout" style="text-align: center;"><a>退出</a></dd>
                    </dl>
                </li>
            </ul>
        </div>

        <!-- 左侧菜单-->
        <div id="mainmenu" class="layui-side layui-side-menu">
            <div class="layui-side-scroll">
                <div class="layui-logo" lay-href="/index/main">
                    <img src="/favicon.ico" alt="logo"
                         style="height: 30px; width: 30px; vertical-align: middle; margin-left: -30px; display: inline-block;">
                    <span style="color: white; font-weight: bold;">税务稽查助手</span>
                </div>
                <div class="navBar layui-side-scroll" id="navBarId"></div>
            </div>
        </div>

        <!-- 页面标签 -->
        <div class="layadmin-pagetabs" id="LAY_app_tabs">
            <div class="layui-icon layadmin-tabs-control layui-icon-prev" layadmin-event="leftPage"></div>
            <div class="layui-icon layadmin-tabs-control layui-icon-next" layadmin-event="rightPage"></div>
            <div class="layui-icon layadmin-tabs-control layui-icon-down">
                <ul class="layui-nav layadmin-tabs-select" lay-filter="layadmin-pagetabs-nav">
                    <li class="layui-nav-item" lay-unselect>
                        <a href="javascript:void(0);"></a>
                        <dl class="layui-nav-child layui-anim-fadein">
                            <dd layadmin-event="closeThisTabs"><a href="javascript:void(0);">关闭当前标签页</a></dd>
                            <dd layadmin-event="closeOtherTabs"><a href="javascript:void(0);">关闭其它标签页</a></dd>
                            <dd layadmin-event="closeRightTabs"><a href="javascript:void(0);">关闭右侧标签页</a></dd>
                            <dd layadmin-event="closeAllTabs"><a href="javascript:void(0);">关闭全部标签页</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
            <div class="layui-tab" lay-unauto lay-allowClose="true" lay-filter="layadmin-layout-tabs"
                 id="LAY_app_toptabs">
                <ul class="layui-tab-title" id="LAY_app_tabsheader"></ul>
                <div class="layui-tab-content"></div> <!-- 这个内容区可忽略，不放内容！ -->
            </div>
        </div>

        <!-- 内容区初始为空 -->
        <div class="layui-body" id="LAY_app_body"></div>

        <!-- 辅助元素，一般用于移动设备下遮罩 -->
        <div class="layadmin-body-shade" layadmin-event="shade"></div>

    </div>
</div>
<script>
    var token = document.getElementById("token").value;
    var userId = document.getElementById("userId").value;
    var test = token + "#" + userId;
    // 存入 localStorage
    console.log("test:::" + test);
    const map = {
        "access_token": test
    };
    localStorage.setItem("LocalData", JSON.stringify(map));
</script>
<script src="/layui/v11/layui.js"></script>
<script src="/js/core.util.js"></script>
<script src="/js/jquery.js"></script>
<script th:inline="none">
    const layer = layui.layer;
    const $ = jQuery = layui.jquery;
    const form = layui.form;
    const element = layui.element;
    const admin = layui.admin;
    const mainmenu = $('#mainmenu');

    layui.config({
        base: '../layui/v2.6/' // 静态资源所在路径
    }).extend({
        index: 'index' // 主入口模块
    }).use(['index', 'element', 'jquery'], function () {

        // 动态计算内容区位置
        function adjustBodyPosition() {
            var headerHeight = $('.layui-header').outerHeight() || 60;
            var tabBarHeight = $('.layadmin-pagetabs').outerHeight() || 50;
            var sideWidth = mainmenu.is(':visible') ? mainmenu.outerWidth() || 200 : 0;

            $('#LAY_app_body').css({
                'top': headerHeight + tabBarHeight + 'px',
                'left': sideWidth + 'px',
                'width': 'calc(100% - ' + sideWidth + 'px)',
                'height': 'calc(100% - ' + (headerHeight + tabBarHeight) + 'px)'
            });
        }

        // 初始化执行
        adjustBodyPosition();
        $(window).resize(adjustBodyPosition);
        element.on('collapse(menu)', function () {
            setTimeout(adjustBodyPosition, 300);
        });
        // 全局Tab缓存
        const tabCache = {};

        // 打开或切换Tab
        function openTab(url, title, icon) {
            var tabId = url;

            // 如果Tab已存在但DOM中不存在（被删除的情况）
            if (tabCache[tabId] && !$('#LAY_app_tabsheader li[lay-id="' + tabId + '"]').length) {
                delete tabCache[tabId]; // 清除无效缓存
            }

            // 创建新Tab（无论是否存在都重新创建）
            element.tabAdd('layadmin-layout-tabs', {
                title: (icon ? '<i class="layui-icon ' + icon + '"></i>' : '') + title,
                id: tabId
            });

            // 移除旧内容（如果存在）
            $('[data-tabid="' + tabId + '"]').remove();

            // 创建新内容
            var $content = $(
                '<div class="layadmin-tabsbody-item" data-tabid="' + tabId + '">' +
                '  <iframe src="' + url + '" frameborder="0" class="layadmin-iframe"></iframe>' +
                '</div>'
            ).hide();

            $('#LAY_app_body').append($content);

            // 更新缓存
            tabCache[tabId] = {
                title: title,
                url: url,
                $content: $content
            };

            // 切换到该Tab
            element.tabChange('layadmin-layout-tabs', tabId);
        }

        // Tab切换事件
        element.on('tab(layadmin-layout-tabs)', function (data) {
            const tabId = $(this).attr('lay-id');
            $('#LAY_app_body .layadmin-tabsbody-item').hide();
            $('[data-tabid="' + tabId + '"]').show();
            resizeIframe(tabId);
        });

        // iframe高度调整
        function resizeIframe(tabId) {
            const $iframe = $('[data-tabid="' + tabId + '"] iframe');
            if ($iframe.length) {
                $iframe.height($('#LAY_app_body').height());
            }
        }

        // 默认打开首页
        openTab('/index/mainclerk', '首页', 'layui-icon-home');
        layui.$(document).on('click', '[layadmin-event="logout"]', function () {
            // 你自己的退出逻辑
            layer.confirm('确定要退出登录吗？', {
                title: '提示'
            }, function (index) {
                // 清除本地 token 等
                // localStorage.removeItem('token');
                CoreUtil.sendGet("/sys/user/logout", null, function () {
                    layui.data('LocalData', null);
                    top.window.location.href = "/index/login";
                });
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            });
        });
    });

    // 搜索菜单方法
    $("#menuSearch").keydown(function (event) {
        if (event.keyCode === 13) {
            FrameWH();
            // 递归获取导航栏无限层级（已兼容 layui-admin 2.6.0）
            var getChild = function (items, ulHtml) {
                ulHtml += '<dl class="layui-nav-child">';
                $.each(items, function (index, child) {
                    // 有子节点
                    if (child.children != null && child.children.length > 0) {
                        ulHtml += '<dd>';
                        ulHtml += '<a href="javascript:void(0);" lay-tips="' + child.title + '">';
                        if (child.icon) {
                            ulHtml += '<i class="layui-icon ' + child.icon + '"></i>';
                        }
                        ulHtml += '<cite>' + child.title + '</cite></a>';

                        // 递归子菜单
                        ulHtml = getChild(child.children, ulHtml);

                        ulHtml += '</dd>';
                    } else {
                        // 没有子节点
                        ulHtml += '<dd>';
                        if (child.target === "_blank") {
                            ulHtml += '<a href="' + child.url + '" target="_blank" lay-tips="' + child.title + '">';
                        } else {
                            ulHtml += '<a lay-href="' + child.url + '" lay-tips="' + child.title + '">';
                        }

                        if (child.icon) {
                            ulHtml += '<i class="layui-icon ' + child.icon + '"></i>';
                        }
                        ulHtml += '<cite>' + child.title + '</cite></a>';
                        ulHtml += '</dd>';
                    }
                });
                ulHtml += '</dl>';
                return ulHtml;
            };
            //查询菜单目录
            CoreUtil.sendGet("/sys/menuSearch/", {"menuName": $("#menuSearch").val()}, function (res) {
                var data = res.data;
                if (data !== "" && data.length > 0) {
                    $("#navBarId").empty();
                    let ulHtml = '<ul class="layui-nav layui-nav-tree layui-left-nav" lay-filter="layadmin-system-side-menu">';
                    if (data.length > 0) {
                        $.each(data, function (index, item) {
                            ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
                            ulHtml += '<a href="javascript:void(0);" lay-tips="' + item.title + '">';
                            if (!CoreUtil.isEmpty(item.icon)) {
                                ulHtml += '<i class="layui-icon ' + item.icon + '"></i>';
                            }
                            ulHtml += '<cite>' + item.title + '</cite>';
                            ulHtml += '</a>';

                            if (item.children != null && item.children.length > 0) {
                                ulHtml += '<dl class="layui-nav-child">';
                                $.each(item.children, function (index, child) {
                                    if (child.children != null && child.children.length > 0 && child.type === 1) {
                                        // 递归子菜单
                                        ulHtml += '<dd><a href="javascript:void(0);" lay-tips="' + child.title + '">';
                                        if (child.icon) {
                                            ulHtml += '<i class="layui-icon ' + child.icon + '"></i>';
                                        }
                                        ulHtml += '<cite>' + child.title + '</cite></a>';
                                        ulHtml += getChild(child.children, "");  // 保留你的递归子菜单逻辑
                                        ulHtml += '</dd>';
                                    } else {
                                        ulHtml += '<dd>';
                                        if (child.target === "_blank") {
                                            ulHtml += '<a href="' + child.url + '" target="_blank" lay-tips="' + child.title + '">';
                                        } else {
                                            ulHtml += '<a lay-href="' + child.url + '" lay-tips="' + child.title + '">';
                                        }
                                        if (!CoreUtil.isEmpty(child.icon)) {
                                            ulHtml += '<i class="layui-icon ' + child.icon + '"></i>';
                                        }
                                        ulHtml += '<cite>' + child.title + '</cite></a></dd>';
                                    }
                                });
                                ulHtml += '</dl>';
                            }

                            ulHtml += '</li>';
                        });
                    }
                    ulHtml += '</ul>';
                    $(".navBar").append(ulHtml);
                    element.init(); // 初始化 Layui 元素
                } else {
                    $("#navBarId").empty();
                }
                top.layer.closeAll();
            });
        }
    });
    // 页面初始化
    $(function () {
        FrameWH();

        //递归获取导航栏无线层级
        function getChild(children) {
            let html = '<dl class="layui-nav-child">';
            $.each(children, function (i, child) {
                if (child.children && child.children.length > 0) {
                    html += '<dd>';
                    html += '<a href="javascript:void(0);" lay-tips="' + child.title + '" lay-direction="2">';
                    if (child.icon) {
                        html += '<i class="layui-icon ' + child.icon + '"></i>';
                    }
                    html += '<cite>' + child.title + '</cite></a>';
                    html += getChild(child.children);
                    html += '</dd>';
                } else {
                    html += '<dd>';
                    if (child.target === '_blank') {
                        html += '<a href="' + child.url + '" target="_blank" lay-tips="' + child.title + '" lay-direction="2">';
                    } else {
                        html += '<a lay-href="' + child.url + '" lay-tips="' + child.title + '" lay-direction="2">';
                    }
                    if (child.icon) {
                        html += '<i class="layui-icon ' + child.icon + '"></i>';
                    }
                    html += '<cite>' + child.title + '</cite></a></dd>';
                }
            });
            html += '</dl>';
            return html;
        }

        CoreUtil.sendGet("/sys/home/", null, function (res) {
            $("#deptName").html(res.data.userInfo.deptName + "-");
            $("#deptId").val(res.data.userInfo.deptId);
            $("#nickName").html(res.data.userInfo.nickName);

            let data = res.data.menus;
            if (data && data.length > 0) {
                let ulHtml = '<ul class="layui-nav layui-nav-tree layui-left-nav" lay-filter="layadmin-system-side-menu">';

                $.each(data, function (index, item) {
                    ulHtml += '<li class="layui-nav-item' + (index === 0 ? ' layui-nav-itemed' : '') + '">';
                    ulHtml += '<a href="javascript:void(0);" lay-tips="' + item.title + '" lay-direction="2">';
                    if (item.icon) {
                        ulHtml += '<i class="layui-icon ' + item.icon + '"></i>';
                    }
                    ulHtml += '<cite>' + item.title + '</cite></a>';

                    if (item.children && item.children.length > 0) {
                        ulHtml += getChild(item.children);
                    }

                    ulHtml += '</li>';
                });

                ulHtml += '</ul>';
                $(".navBar").empty().append(ulHtml);
                element.init(); // 重新渲染菜单
            }
        });

        $(document).on('click', '.menuNvaBar', function () {
            var dataid = $(this);
            if ($(".layui-tab-title li[lay-id]").length <= 0) {
                if (CoreUtil.isEmpty(dataid.attr("data-icon"))) {
                    var title = "<span>"
                        + dataid.attr("data-title")
                        + "</span>";
                    active.tabAdd(dataid.attr("data-url"),
                        dataid.attr("data-id"), title);
                } else {
                    var title = "<span>"
                        + dataid.attr("data-title")
                        + "</span>";
                    title = '<i class="layui-icon '
                        + dataid.attr("data-icon")
                        + '"></i> ' + title;
                    active.tabAdd(dataid.attr("data-url"),
                        dataid.attr("data-id"), title);
                }
            } else {
                var isData = false;
                $
                    .each(
                        $(".layui-tab-title li[lay-id]"),
                        function () {
                            if ($(this).attr("lay-id") == dataid.attr("data-id")) {
                                isData = true;
                            }
                        })
                if (isData === false) {
                    if (CoreUtil.isEmpty(dataid
                        .attr("data-icon"))) {
                        var title = "<span>"
                            + dataid.attr("data-title")
                            + "</span>";
                        active.tabAdd(dataid.attr("data-url"),
                            dataid.attr("data-id"), title);
                    } else {
                        var title = "<span>"
                            + dataid.attr("data-title")
                            + "</span>";
                        title = '<i class="layui-icon '
                            + dataid.attr("data-icon")
                            + '"></i> ' + title;
                        active.tabAdd(dataid.attr("data-url"),
                            dataid.attr("data-id"), title);
                    }
                }
            }
            active.tabChange(dataid.attr("data-id"));
        });
    });
    //触发事件
    var active = {
        tabAdd: function (url, id, title) {
            if (url !== "" && url !== null && url !== undefined) {
                element
                    .tabAdd(
                        'tab',
                        {
                            title: title,
                            content: '<iframe data-frameid="'
                                + id
                                + '" frameborder="0" name="content" width="100%" src="'
                                + url + '"></iframe>',
                            id: id
                        })
            }
            FrameWH();//计算框架高度
        },
        tabChange: function (id) {
            //切换到指定Tab项
            element.tabChange('tab', id); //切换到：用户管理
            $("iframe[data-frameid='" + id + "']").attr("src",
                $("iframe[data-frameid='" + id + "']").attr("src"))//切换后刷新框架
        },
        tabDelete: function (id) {
            element.tabDelete("tab", id);//删除
        }
    };

    function FrameWH() {
        var h = $(window).height() - 41 - 10 - 35 - 10 - 44 - 10;
        $("iframe").css("height", h + "px");
    }

    //推出登录
    $("#logout").click(function () {
        logoutDialog();
    });

    //删除前确认对话框
    const logoutDialog = function () {
        layer.open({
            content: '确定要退出登录么？',
            yes: function (index) {
                layer.close(index); //如果设定了yes回调，需进行手工关闭
                CoreUtil.sendGet("/sys/user/logout", null, function (res) {
                    layui.data('LocalData', null);
                    top.window.location.href = "/index/login";
                });
            }
        });
    }


</script>
</body>
</html>